package com.jtbd.service;

import com.jtbd.domain.Customers;
import com.jtbd.dto.request.CustomerItemCreateHttpDto;
import com.jtbd.handler.GetSecurityUserHandler;
import com.jtbd.service.campus.CustomerChildrenRelationService;
import com.jtbd.service.campus.CustomerChildrenService;
import com.jtbd.service.impl.campus.CustomersServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
public class CustomerManagementService {
    @Resource
    private CustomersServiceImpl customersService;
    @Resource
    private CustomerChildrenService customerChildrenService;
    @Resource
    private CustomerChildrenRelationService customerChildrenRelationService;

    /**
     * 新增客户信息
     *
     * @param itemCreateHttpDto 客户信息
     * @return 新增成功返回客户ID，新增失败返回null
     */
    @Transactional
    public Long addCustomer(CustomerItemCreateHttpDto itemCreateHttpDto) {
        Customers customers = itemCreateHttpDto.getCustomers();
        // 当前操作人
        Long operatorUserId = GetSecurityUserHandler.getSecurityUserId();
        if (!ObjectUtils.isEmpty(operatorUserId)) {
            customers.setCreateId(operatorUserId);
            customers.setUpdateId(operatorUserId);
        }
        if (!ObjectUtils.isEmpty(customers)) {
            Long customerId = customersService.addCustomer(customers);
            if (!ObjectUtils.isEmpty(customerId)) {
                customerChildrenService.addCustomerChildren(itemCreateHttpDto.getCustomerChildrens(), customerId, operatorUserId);
            }
            return customerId;
        }
        return null;
    }
}
